//
//  GoodsFirstTableViewCell.m
//  Students affairs
//
//  Created by eliot on 16/3/10.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "GoodsFirstTableViewCell.h"
@implementation GoodsFirstTableViewCell


- (void)setupCellWithDic:(NSDictionary *)dic {
    [self asdasd];
}


- (void)asdasd {
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.frame = CGRectMake(0, 0, Width, 222/320.0*Width);
    //创建页点
    UIPageControl *tempPage = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 222/320.0*Width - 20, self.frame.size.width,222/320.0*Width - 20)];
    
    [tempPage setBackgroundColor:[UIColor clearColor]];
    [tempPage setAlpha:1];
    self.pageControl = tempPage;
    //[tempPage release];
    //初始化数组,将图片压入数组
    self.imageArray = [NSMutableArray array];
    
    //将滚动视图和翻页视图添加到视图中
    [self addSubview:self.scrollView];
    
    [self addSubview:self.pageControl];
    
    [self createsCrollView:nil];
}


#pragma ------创建视图区域----------
//创建滚动视图区域
- (void)createsCrollView:(id)sender {
    //设置委托
    self.scrollView.delegate = self;
    //将是否取消对内容视图触摸的反应设为NO
    self.scrollView.canCancelContentTouches = NO;
    //设置滚动条类型
   // self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    //是否可以缩放
    self.scrollView.scrollEnabled = YES;
    //是否可以进行页面切换
    self.scrollView.pagingEnabled = YES;
    NSUInteger page = 0;//为了记录页数
    int originX = 0;
    //填充滑动视图内容
    for (UIImage *image in self.imageArray) {
        //首先创建一个视图
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectZero];
        imageview.contentMode = UIViewContentModeScaleToFill;
        //为这个图片设置适当位置
        CGRect rect ;
        rect.origin.x = originX;
        rect.origin.y = 0;
        rect.size.width = Width;
        rect.size.height = 222/320.0*Width;
        imageview.frame = rect;
        //设置图片为背景
        imageview.image = image;
        //添加到视图中
        [self.scrollView addSubview:imageview];
        //确定下张图片开始的x坐标
        originX += Width;
        //记录页数
        page++;
    }
    //设置总页数
    self.pageControl.numberOfPages = page;
    //设置默认页为首页
    self.pageControl.currentPage = 0;
    self.pageControl.tag = 100;
    //为页数控制关联方法
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    //显示适当区域
    [self.scrollView setContentSize:CGSizeMake(originX, 222/320.0*Width)];
}
- (void)repartScroll {
    if (self.pageControl.currentPage == 3) {
        self.pageControl.currentPage = 0;
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }else{
        self.pageControl.currentPage = self.pageControl.currentPage + 1;
    }
    CGRect rect;
    rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;
    rect.origin.y = 0;
    rect.size.width = Width;
    rect.size.height = 222/320.0*Width;
    //设置滚动视图的可视区域
    [self.scrollView scrollRectToVisible:rect animated:YES];
    
}

//点击小点切换页面
- (void)changePage:(id)sender {
    //创建一个区域
    CGRect rect;
    rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;
    rect.origin.y = 0;
    rect.size.width = self.scrollView.frame.size.width;
    rect.size.height = self.scrollView.frame.size.height;
    //设置滚动视图的可视区域
    [self.scrollView scrollRectToVisible:rect animated:YES];

}

//重写滑动结束后的反应，就是把pageConteol的当前页跟随滑动而动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //取滚动视图区域的宽
    float orginX = self.scrollView.frame.size.width;
    //利用当前视图显示区域到view.frame的偏移量，来换算获取当前处在第几页
    int page = ((self.scrollView.contentOffset.x - orginX))/orginX +1;
    //当前页改为这一页。
    self.pageControl.currentPage = page;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
